package com.fzhucc.utils;

import javax.crypto.Cipher;
import javax.crypto.KeyGenerator;
import javax.crypto.SecretKey;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;
import java.nio.charset.StandardCharsets;
import java.security.*;
import java.util.Base64;

/**
 * AES解密
 *
 * @Author: dawei
 * @Date: 2019.3.26 14:41
 */
public class AESUtil {
    /***
     * 修改项目密码链接传递数据加密Key
     */
    public static final String FOLDER_RESET_ENCRYPTION_KEY = "Reset0Folder1Encryption";

//    static {
//        Security.addProvider(new BouncyCastleProvider());
//    }

    public static String genAesSecret(){
        try {
            KeyGenerator kg = KeyGenerator.getInstance("AES");
            kg.init(128);
            SecretKey secretKey = kg.generateKey();
            byte[] b = secretKey.getEncoded();
            return Base64.getEncoder().encodeToString(b);
        } catch (NoSuchAlgorithmException e) {
//            e.printStackTrace();
            return null;
        }
    }

    /**
     * AES加密
     *
     * @param str
     * @param key
     * @return
     */
    public static String encodeString(String str, String key) {
        if (str == null || key == null) {
            return null;
        }
        try {
            SecretKey secretKey = new SecretKeySpec(Base64.getDecoder().decode(key), "AES");

            byte[] enCodeFormat = secretKey.getEncoded();
            SecretKeySpec k = new SecretKeySpec(enCodeFormat, "AES");

            Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding");
            cipher.init(Cipher.ENCRYPT_MODE, k);

            byte[] dataArray = str.getBytes(StandardCharsets.UTF_8);
            byte[] encode = cipher.doFinal(dataArray);

            return Base64.getEncoder().encodeToString(encode);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * 解密
     *
     * @param str
     * @param key
     * @return
     */
    public static String decrypt(String str, String key) {
        if (str == null || key == null) {
            return null;
        }
        try {
            SecretKey secretKey = new SecretKeySpec(Base64.getDecoder().decode(key), "AES");
            byte[] byteKey = secretKey.getEncoded();
            Key key1 = new SecretKeySpec(byteKey, "AES");
            Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding");
            cipher.init(Cipher.DECRYPT_MODE, key1);
            byte[] result = cipher.doFinal(Base64.getDecoder().decode(str));
            return new String(result);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    public static byte[] decrypt(byte[] content, byte[] keyByte, byte[] ivByte) throws InvalidAlgorithmParameterException {
        try {
            Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding", "BC");

            //生成iv
            Key sKeySpec = new SecretKeySpec(keyByte, "AES");
            AlgorithmParameters parameters = AlgorithmParameters.getInstance("AES");
            parameters.init(new IvParameterSpec(ivByte));

            //初始化
            cipher.init(Cipher.DECRYPT_MODE, sKeySpec, parameters);
            return cipher.doFinal(content);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }
}
